//
//  AddOftenRunRouteViewController.m
//  阿帕
//
//  Created by mac on 2020/1/4.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "AddOftenRunRouteViewController.h"
#import "CYCityChooseView.h"

@interface AddOftenRunRouteViewController ()<CYChooseRegionDelegate>
{
    NSString *_consignorProvinceCode;//装货地省份编码
    NSString *_consignorCityCode;
    NSString *_consignorCountyCode;
    NSString *_consignorAddress;

    NSString *_consigneeProvinceCode;//收货地省编码
    NSString *_consigneeCityCode;
    NSString *_consigneeCountyCode;
    NSString *_consigneeAddress;
    
    NSInteger _addressType;
}

@end

@implementation AddOftenRunRouteViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _addressType = 0;
    _consignorProvinceCode = @"";
    _consignorCityCode = @"";
    _consignorCountyCode = @"";
    _consigneeProvinceCode = @"";
    _consigneeCityCode = @"";
    _consigneeCountyCode = @"";
    
    
    [self setupBaseView];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"新增常跑路线";
    
    self.view.backgroundColor = BackgroundColor;
    
    UIView *whiteBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, 90)];
    whiteBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteBgView];
    
    NSArray *formsTitleArr = @[@"起始地",@"目的地"];
    
    //表单区域
    for (int i=0; i<formsTitleArr.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 45*i, ScreenW, 45)];
        [whiteBgView addSubview:bgView];
        
        //标题
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 80, 45)];
        textLabel.text = formsTitleArr[i];
        textLabel.textColor = Dark_TextColor;
        textLabel.font = font(14);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        //分割线
        UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 45-1, ScreenW - 30, 1)];
        hLine.backgroundColor = BackgroundColor;
        [bgView addSubview:hLine];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(ScreenW-225, 0, 200, bgView.height);
        button.tag = 100 + i;
        [button setTitle:@"请选择" forState:UIControlStateNormal];
        button.titleLabel.font = font(14);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:button];
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenW-20, 17, 6, 11)];
        arrow.image = image(@"gcy_icon_hd_rarrow");
        [bgView addSubview:arrow];
    }
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setTitle:@"新增" forState:UIControlStateNormal];
    [addBtn setBackgroundColor:Theme_ColorValue];
    cut(addBtn, BTN_BORDER_RADIUS);
    addBtn.tag = 102;
    [addBtn addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.frame = CGRectMake(15, whiteBgView.cy_bottom + 15, ScreenW - 30, 45);
    [self.view addSubview:addBtn];
}

#pragma mark - CYChooseRegionDelegate
- (void)getAllInfoProvinceCode:(NSString *)provinceCode provinceName:(NSString *)provinceName cityCode:(NSString *)cityCode cityName:(NSString *)cityName countyCode:(NSString *)countyCode countyName:(NSString *)countyName {
    
    if ([DCSpeedy isBlankString:countyCode] == YES) {
        [XSInfoView showInfo:@"地址请精确到区/县" onView:self.view];
        
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 1;
        [CYCityChooseView shared].delegate = self;
        
        return;
    }
    
    NSString *address = CJStringWithFormat(@"%@%@%@",provinceName,cityName,countyName);
    
    if (_addressType == 0) {
        _consignorProvinceCode = provinceCode;
        _consignorCityCode = cityCode;
        _consignorCountyCode = countyCode;
        _consignorAddress = address;
        
        UIButton *button = [self.view viewWithTag:100];
        [button setTitle:address forState:UIControlStateNormal];
        
    } else {
        _consigneeProvinceCode = provinceCode;
        _consigneeCityCode = cityCode;
        _consigneeCountyCode = countyCode;
        _consigneeAddress = address;
        
        UIButton *button = [self.view viewWithTag:101];
        [button setTitle:address forState:UIControlStateNormal];
    }
}


- (void)btnEventClick:(UIButton *)button {
    WEAKSELF
    
    if (button.tag == 100) {
        
        _addressType = 0;
        
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 1;
        [CYCityChooseView shared].delegate = self;
        [CYCityChooseView shared].isTwoLevel = NO;
        
    } else if (button.tag == 101) {
        
        _addressType = 1;
        
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 1;
        [CYCityChooseView shared].delegate = self;
        [CYCityChooseView shared].isTwoLevel = NO;
        
    } else {
        //新增
        if ([DCSpeedy isBlankString:_consignorAddress] == YES || [DCSpeedy isBlankString:_consigneeAddress] == YES) {
            [XSInfoView showInfo:@"请选择地址" onView:self.view];
            return;
        }
        
        NSDictionary *parmas = @{
            @"consignorProvinceCode": _consignorProvinceCode,
            @"consignorCityCode": _consignorCityCode,
            @"consignorCountyCode": _consignorCountyCode,
            @"consigneeProvinceCode": _consigneeProvinceCode,
            @"consigneeCityCode": _consigneeCityCode,
            @"consigneeCountyCode": _consigneeCountyCode,
            @"consignorDetailAddress": _consignorAddress,
            @"consigneeDetailAddress": _consigneeAddress,
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        
        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], COMMON_ROUTEINFO) withParameter:parmas withVC:self netName:@"新增常跑路线" success:^(id response, NSString *msg) {
                        
            [XSInfoView showInfo:msg onView:weakSelf.view];
            
            if (self.addOftenRunRouteBlock) {
                self.addOftenRunRouteBlock(YES);
            }
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
    }
}

@end
